package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.Customer;
import com.example.demo.entity.OrderBase;
import com.example.demo.repository.CustomerRepository;
import com.example.demo.service.CustomerService;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CustomerController {

    @Autowired
    CustomerService customerService;

    // 通过ID查找客户
    @GetMapping("/customerFind")
    public Map<String,Object> customerFind(@RequestParam("id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        Customer customer = customerService.getCustomer(id);
        dataMap.put("status",0);
        dataMap.put("data",customer);
    return dataMap;
    }
    // 新增修改用户
    @PostMapping("/customer")
    public Map<String,Object> customer(@RequestParam Map customer, HttpServletRequest request){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        Customer save = customerService.insertCustomer(request,customer);
        dataMap.put("status",0);
        dataMap.put("message","保存成功");
        dataMap.put("data",save);
        return dataMap;
    }

    /*
    *  这是分页查询订单数量
    *   parms => page:第一个页
    *            size:要几条
    *            customerQuery:订单里面的参数集合(id.orderName)
    * */
    @GetMapping("/customerQuery")
    public Map<String,Object> customerQuery( @RequestParam Map customerQuery,HttpSession session){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        JSONObject customerJson = (JSONObject) JSONObject.parse(customerQuery.get("customerQuery").toString());
        int page = Integer.valueOf(customerQuery.get("page").toString());
        int size = Integer.valueOf(customerQuery.get("size").toString());
        System.out.println(customerJson);
        String userId = session.getAttribute("user").toString();
        Page<Customer> datas = customerService.findCustomerCriteria(page, size,customerJson,userId);
        dataMap.put("data",datas);
        dataMap.put("status",0);
        return dataMap;

    }
    /*
   *  这是通过订单ID删除指定的客户
   *   parms => id:要删除的ID
   * */
    @GetMapping("/customerDelete")
    public Map<String,Object> customerDelete( @RequestParam(value = "id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        if( id == null ){
            dataMap.put("errMsg","无效的ID");
            dataMap.put("status",0);
        }else{
            Boolean dataBool = customerService.customerDelete(id);
            dataMap.put("data",dataBool);
            dataMap.put("status",0);
        }
        return dataMap;
    }

}
